package com.cndw.kungfu.event.once;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.kungfu.comm.tencent.QQDC;
import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;

public class SystemCloudAlarm implements Runnable {

	private static final String cmd = "/usr/local/services/CloudDCAgent_L5-1.0/alarm/cloud_alarm";

	private static final String eql = "-";

	private static final String appIdStr = String.valueOf(QQDC.appId);

	public static final String db_d = "db_d";

	public static final String db_c = "db_c";
	
	public static final String some_e = "some_e";

	private static final Map<String, Integer> lastTimes = new ConcurrentHashMap<String, Integer>();

	private String[] parms;

	private SystemCloudAlarm(String... parms) {
		this.parms = parms;
	}

	@Override
	public void run() {

		try {
			long t = System.currentTimeMillis();
			Process data = Runtime.getRuntime().exec(parms);
			data.waitFor();
			LogUtil.getMain().debug("alarm : " + (System.currentTimeMillis() - t) + " -> ret " + data.exitValue());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void send(String content, String obj) {
		try {
			if (!QQDC.getInstance().isOpenDC()) {
				return;
			}
			if (lastTimes.containsKey(obj) && lastTimes.get(obj) + 1800 >= TimeProcesser.getUnixTime()) {
				return;
			}
			lastTimes.put(obj, TimeProcesser.getUnixTime());
			SystemCloudAlarm alarm = new SystemCloudAlarm(cmd, appIdStr, ConstantsConfig.getSysCgSvrId() + eql + content, "-o " + obj);
			Context.getTimerServer().execute(alarm);
			LogUtil.getMain().error("alarm " + obj + " -> " + content);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
